﻿using System;
using System.Text;

namespace configstore.CSM.plugins
{
    using SanlieHash.BlockCipher;
    using configstore.CSM.Common;
    using TouchSocket.Core;
    using TouchSocket.Http;
    internal class TestPlug : PluginBase, IHttpPlugin
    {
        public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
        {
            var request = e.Context.Request;//http请求体
            var response = e.Context.Response;//http响应体
            response.Headers.Add(HttpHeaders.Server, HttpCommon.SERVER);

            if (request.IsGet() && request.UrlEquals("/store/config/hi"))
            {
                //直接响应文字
                await response
                        .SetStatus(200, "success")
                        .FromStreamAsync("hello-world.txt", new MemoryStream(Encoding.UTF8.GetBytes("hello world !")));
                await response.AnswerAsync();//直接回应
                return;
            }
            if (request.IsGet() && request.UrlEquals("/store/config/hi-crypto"))
            {
                //直接响应文字
                await response
                        .SetStatus(200, "success")
                        .FromStreamAsync("hello-world.txt", new MemoryStream(AES.EncryptBytes("111", Encoding.UTF8.GetBytes("hello world !"))));
                await response.AnswerAsync();//直接回应
                return;
            }
            //无法处理，调用下一个插件
            await e.InvokeNext();
        }
    }
}
